package com.ciaojian.bytedance.authcontroller;

import com.alibaba.fastjson.JSONObject;
import com.ciaojian.bytedance.service.ByteDanceMiniAppAuthService;
import com.ciaojian.core.bytedance.vo.ByteMiniAccessToken;
import com.ciaojian.bytedance.vo.bytedance.DyLoginVo;
import com.ciaojian.core.model.vo.DyUserVo;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "授权小程序登录")
@Slf4j
@RestController
@RequestMapping("/v2/auth")
public class ByteDanceV2AuthController {


    @Autowired
    private ByteDanceMiniAppAuthService byteDanceMiniAppAuthService;


    /**
     * 小程序授权
     */
    @ApiOperation("小程序授权")
    @GetMapping
    public ByteMiniAccessToken auth() {
        return byteDanceMiniAppAuthService.authMiniApp();
    }

    /**
     * 用户授权，把用户信息保存，并且登录
     */
    @ApiOperation("用户授权，把用户信息保存，并且登录")
    @PostMapping("/login")
    public JSONObject login(@RequestBody DyLoginVo dyLoginVo) {
        return byteDanceMiniAppAuthService.login(dyLoginVo);
    }

    /**
     * 用户信息登录保存
     */
    @PostMapping("/saveUser")
    public Result saveUserInfo(@RequestBody DyUserVo dyUserVo) {
       return byteDanceMiniAppAuthService.saveUserInfo(dyUserVo);
    }

    /**
     * 用户头像等信息保存
     */
    @PostMapping("/userPortrait")
    public Result userPortrait(@RequestBody DyUserVo dyUserVo) {
        return byteDanceMiniAppAuthService.userPortrait(dyUserVo);
    }

    /**
     * 用户信息详情
     */
    @GetMapping("/userBydyopenid")
    public Result userBydyopenid(String dyOpenid) {
        return byteDanceMiniAppAuthService.getUserBydyopenid(dyOpenid);
    }
}
